<template>
  <div class="login">
    <myheader :title="title"></myheader>
    <div class="main">
      <form ref="form" :model="form">
      <div>
        昵称：<input type="text" name="username" v-model="form.username" />
      </div>
      <div>手机：<input type="tel" name="tel" v-model="form.tel" /></div>
      <div>
        密码：<input type="password" name="password" v-model="form.password" />
      </div>
    </form>
    <button @click="login">登 录</button>
    </div>
  </div>
</template>

<script>
import myheader from "@/components/myheader";

// 正则验证
// 手机号
const regtel = /^1[3|5|6|7|8|9]\d{9}$/;
// 密码
const regpass = /^\d{6,}$/;

export default {
  name: "login",
  components: { myheader },
  data() {
    return {
      title: "登录页面",
      form: {
        username: "",
        password: "",
        tel: "",
      },
    };
  },
  methods: {
    login() {
      if (
        this.form.username == "" ||
        this.form.tel == "" ||
        this.form.password == ""
      ) {
        this.$message.show("输入内容不能为空");
        setTimeout(()=>{
          this.$message.hide()
        },1000)
        return false
      }
      if (!regtel.test(this.form.tel)) {
        this.$message.show("手机号输入格式错误");
         setTimeout(()=>{
          this.$message.hide()
        },1000)
      } else if (!regpass.test(this.form.password)) {
        this.$message.show("密码输入格式错误");
         setTimeout(()=>{
          this.$message.hide()
        },1000)
      } else {
        this.$store.commit("changeuser", this.form);
        this.$router.push('/home');
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  .main{
    width: 70%;
    height: 200px;
    border: 1px solid gainsboro;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    form {
    
    div {
      width: 100%;
      height: 30px;
      margin: 10px 0;
      input {
        width: 60%;
        height: 100%;
        border: 1px solid gainsboro;
        text-indent: 1em;
      }
    }
  }
  button {
    width: 50%;
    height: 40px;
    border-radius: 6px;
    background-color: rgb(167, 243, 131);
    color: #fff;
    font-size: 18px;
    outline: 0;
    border: 0;
  }
  }
}
</style>